/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wuhenzhizao.titlebar.widget;

import ohos.agp.components.AttrSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Utils
 *
 * @since 2021-06-08
 */
public class Utils {
    private static final String TAG = "RippleView";
    private static final int NUM_0XFF = 0xFF;
    private static final int NUM_16 = 16;
    private static final int NUM_8 = 8;
    private static HiLogLabel sLogLabel;
    private static final int DOMAIN = 0x001;

    private static HiLogLabel createLogLabel() {
        if (sLogLabel == null) {
            sLogLabel = new HiLogLabel(HiLog.LOG_APP, DOMAIN, TAG);
        }
        return sLogLabel;
    }

    /**
     * debug
     *
     * @param format  format
     * @param objects objects
     */
    public static void debug(String format, Object... objects) {
        HiLog.debug(createLogLabel(), format, objects);
    }

    /**
     * error
     *
     * @param format  format
     * @param objects objects
     */
    public static void error(String format, Object... objects) {
        HiLog.error(createLogLabel(), format, objects);
    }

    /**
     * getFloatFromAttr
     *
     * @param attrSet      attrSet
     * @param name         name
     * @param defaultValue defaultValue
     * @return float
     */
    public static float getFloatFromAttr(AttrSet attrSet, String name, float defaultValue) {
        float value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getFloatValue();
        }
        return value;
    }

    /**
     * getColorFromAttr
     *
     * @param attrSet      attrSet
     * @param name         name
     * @param defaultValue defaultValue
     * @return int
     */
    public static int getColorFromAttr(AttrSet attrSet, String name, int defaultValue) {
        int value = defaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            value = attrSet.getAttr(name).get().getColorValue().getValue();
        }
        return value;
    }

    /**
     * getBooleanFromAttr
     *
     * @param attrSet        attrSet
     * @param name           name
     * @param isDefaultValue isDefaultValue
     * @return boolean
     */
    public static boolean getBooleanFromAttr(AttrSet attrSet, String name, boolean isDefaultValue) {
        boolean isValue = isDefaultValue;
        if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
            isValue = attrSet.getAttr(name).get().getBoolValue();
        }
        return isValue;
    }

    /**
     * getMax
     *
     * @param radius radius
     * @return int
     */
    public static int getMax(int... radius) {
        if (radius.length == 0) {
            return 0;
        }
        int max = radius[0];
        for (int maxs : radius) {
            if (maxs > max) {
                max = maxs;
            }
        }
        return max;
    }

    /**
     * red
     *
     * @param color color
     * @return int
     */
    public static int red(int color) {
        return (color >> NUM_16) & NUM_0XFF;
    }

    /**
     * green
     *
     * @param color color
     * @return int
     */
    public static int green(int color) {
        return (color >> NUM_8) & NUM_0XFF;
    }

    /**
     * blue
     *
     * @param color color
     * @return int
     */
    public static int blue(int color) {
        return color & NUM_0XFF;
    }

    /**
     * invokeStringValue
     *
     * @param property     property
     * @param defaultValue defaultValue
     * @return String
     */
    public static String invokeStringValue(String property, String defaultValue) {
        String value = defaultValue;
        try {
            Class clazz = Class.forName("ohos.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class, String.class);
            value = (String) method.invoke(null, property, defaultValue);
        } catch (ClassNotFoundException classnotfoundexception) {
            error("occured ClassNotFoundException");
        } catch (NoSuchMethodException nosuchmethodexception) {
            error("occured NoSuchMethodException");
        } catch (IllegalAccessException e) {
            error("occured IllegalAccessException");
        } catch (InvocationTargetException e) {
            error("occured InvocationTargetException");
        }
        return value;
    }
}
